Natural resources, such as oil or gas, residing in a subterranean formation can be recovered by drilling a wellbore that penetrates the formation. A variety of fluids are used in both drilling and completing the wellbore and in resource recovery. Example fluids include drilling fluid, also called mud, that is pumped into the wellbore during drilling and similar operations, spacer, which helps flush residual drilling fluid from the wellbore, cement, which typically lines at least part of the finished wellbore and is placed after flushing with a spacer, and fracturing fluids, which may be used to enhance oil or natural gas recovery. Although some parts of the wellbore lie near the surface, the majority of it is deep underground, where harsh conditions are found. In addition, any problems with a downhole fluid can be difficult to detect or correct because the fluid may be far away from the surface and relatively inaccessible, particularly in the case of cement that has set and is no longer a fluid. Accordingly, downhole fluids and additives for downhole fluids should be able to tolerate harsh conditions or avoid or ameliorate problems that may develop in downhole fluids.